#define _CRT_SECURE_NO_WARNINGS 1

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    struct ListNode* tail = NULL;
    struct ListNode* start = NULL;
    if (list1 == NULL)
    {
        return list2;
    }
    if (list2 == NULL)
    {
        return list1;
    }

    if (list1->val < list2->val)
    {
        start = list1;
        list1 = list1->next;
    }
    else
    {
        start = list2;
        list2 = list2->next;
    }
    tail = start;
    while (list1 && list2)
    {
        if (list1->val < list2->val)
        {
            tail->next = list1;
            list1 = list1->next;
        }
        else
        {
            tail->next = list2;
            list2 = list2->next;
        }
        tail = tail->next;
    }
    if (list1)
    {
        tail->next = list1;
    }
    else
    {
        tail->next = list2;
    }
    return start;
}